package com.example.test_activity.MVP.view;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.test_activity.MVP.presenter.ILoginPresenter;
import com.example.test_activity.MVP.presenter.LoginPresenter;
import com.example.test_activity.R;

public class LoginActivity extends Activity implements ILogin,View.OnClickListener{

    public Button button;
    public EditText ed_code;
    public ImageView image;
    private EditText ed_account;
    private EditText ed_pass;

    private ILoginPresenter loginPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        init();
        loginPresenter = new LoginPresenter(this);

        loginPresenter.getCodeImg();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        loginPresenter.destroy();
        loginPresenter = null;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id)
        {
            case R.id.btn:
                String account = ed_account.getText().toString();
                String password = ed_pass.getText().toString();
                String code = ed_code.getText().toString();
                button.setEnabled(false);
                loginPresenter.doLogin(account,password,code);
                SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
                editor.putString("account",account);
                editor.putString("password",password);
                editor.apply();
                break;
            case R.id.img:
                loginPresenter.getCodeImg();
                break;
        }
    }

    private void init() {
        button = (Button) findViewById(R.id.btn);
        image = (ImageView) findViewById(R.id.img);

        ed_code = (EditText) findViewById(R.id.edit);
        ed_account = (EditText) findViewById(R.id.ed_account);
        ed_pass = (EditText) findViewById(R.id.ed_pass);

        button.setOnClickListener(this);
        image.setOnClickListener(this);

        SharedPreferences sharedPreferences = getSharedPreferences("data",MODE_PRIVATE);
        String account = sharedPreferences.getString("account","");
        String password = sharedPreferences.getString("password","");
        ed_account.setText(account);
        ed_pass.setText(password);
    }

    //跳转界面
    private void parcelableClick(String user) {
        Intent intent = new Intent(LoginActivity.this, ViewActivity.class);
        intent.putExtra("user", user);
        startActivity(intent);
        finish();
    }

    @Override
    public void showCodeImg(Bitmap bitmap) {
        image.setImageBitmap(bitmap);
    }

    @Override
    public void login(String user) {
        if (!user.equals(""))
        {
            parcelableClick(user);
        }else {
            loginPresenter.doClear();
            loginPresenter.getCodeImg();
        }
    }

    @Override
    public void clear() {
        ed_pass.setText("");
        ed_code.setText("");
        button.setEnabled(true);
        Toast.makeText(this,"验证码错误",Toast.LENGTH_SHORT).show();
    }
}
